package jt.classic.app.newframe;

import jt.classic.system.ISystem;
import jt.classic.system.SimpleAction;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.module.ModuleManager;
import jt.classic.system.user.IUser;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import java.sql.Connection;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 系统框架加载时其它自定义处理控制器
 *
 * @author mido
 *
 */
public class NewFrameAction extends SimpleAction {
    /**
     * 加载session对象
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward loadSession(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        IUser user = ISystem.getSessionUser(request);

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ModuleManager manager = new ModuleManager(conn);

            List list = user != null ? manager.getMyModuleList(user) : null;

            String jsonString = "{success: false, list:{}}";

            if (list != null) {
                String script = ModuleManager.getAllModuleJson(list);

                jsonString = "{success: true, list:";
                jsonString += script;
                jsonString += "}";
            }

            ISystem.createSessionObject(request, "SESSION_MODULEJSON", jsonString);

            request.setAttribute("result", "{success:true}");
        } catch (Exception ex) {
            request.setAttribute("result", "{success:false}");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }
}
